package com.tongcao.cn.exception;

import com.tongcao.cn.exception.MyException;
import com.tongcao.cn.helper.CommonHelper;
import com.tongcao.cn.util.PropertiesUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Author:tongcao
 * @Date:2018/8/24 15:55
 * Description:全局异常处理
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    private static Properties sysErrors;

    @Resource
    CommonHelper commonHelper;


    public static void initProps() {
        sysErrors = PropertiesUtils.getProperty("error.properties");
    }

    /**
     * 应用到所有@RequestMapping注解方法，在其执行之前初始化数据绑定器
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
    }

    /**
     * 把值绑定到Model中，使全局@RequestMapping可以获取到该值
     *
     * @param model
     */
    @ModelAttribute
    public void addAttributes(Model model) {
        model.addAttribute("author", "tongcao很帅");
    }

    /**
     * 所有异常报错
     *
     * @param request
     * @param exception
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = Exception.class)
    public Map allExceptionHandler(HttpServletRequest request,
                                   Exception exception) throws Exception {

        Map map = new HashMap();
        handleException(exception, map);
        return map;
    }


    /**
     * 统一处理
     *
     * @param e
     */
    private void handleException(Exception e, Map<String, Object> map) {
        initProps();
        String eName = e.getClass().getName();
        if (eName.equals("org.springframework.web.util.NestedServletException")) {
            String str = PropertiesUtils.encodeToGBK(commonHelper.getErrorProps(eName));
            String[] errArray = StringUtils.split(str, "|");
            map.put("errorCode", errArray[1]);
            map.put("errorMsg", errArray[0]);
        } else if (e.getClass().getSimpleName().equals("")) {

        } else {
            map.put("errorCode", "404");
            map.put("errorMsg", "系统异常");
        }
    }

    /**
     * 所有异常报错
     *
     * @param exception
     * @return
     * @throws Exception
     */
//    @ExceptionHandler(value = MyException.class)
//    public Map myExceptionHandler(MyException exception) {
//        Map map = new HashMap();
//        map.put("code", exception.getCode());
//        map.put("msg", exception.getErrMsg());
//        return map;
//    }
    @ExceptionHandler(value = MyException.class)
    public ModelAndView myExceptionHanlder(MyException e) {
        String message = e.getMessage();
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("errMSg", message);
        modelAndView.setViewName("/error");
        return modelAndView;
    }
}
